<?php

namespace app\admin\model;

use think\Model;

class TaskJob extends Model
{
    // 表名
    protected $name = 'task_job';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'called_text',
        'calltime_text',
        'callstate_text',
        'ivrstate_text',
        'queuestate_text'
    ];
    

    
    public function getCalledList()
    {
        return ['0' => '', '1' => __('√')];
    }     

    public function getCallstateList()
    {
        return ['success' => __('Callstate success'),'fail' => __('Callstate fail'),'none' => __('Callstate none')];
    }     

    public function getIvrstateList()
    {
        return ['started' => __('Ivrstate started'),'finished' => __('Ivrstate finished'),'none' => __('Ivrstate none')];
    }     

    public function getQueuestateList()
    {
        return ['queued' => __('Queuestate queued'),'recepted' => __('Queuestate recepted'),'none' => __('Queuestate none')];
    }     


    public function getCalledTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['called'];
        $list = $this->getCalledList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCalltimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['calltime'];
        if ($value <= 0) {
            return '';
        }
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCallstateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['callstate'];
        $list = $this->getCallstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIvrstateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['ivrstate'];
        $list = $this->getIvrstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getQueuestateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['queuestate'];
        $list = $this->getQueuestateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCalltimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function task()
    {
        return $this->belongsTo('task', 'tid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
